/**
 * 
 */
package lu.med.praxiswahl.wahlumed.client.addressbook;

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.border.LineBorder;

import lu.med.praxiswahl.wahlumed.client.wclient.Main;

/**
 * @author PatrickPierre
 * 
 */
public class AddressBookView extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2005802935300106209L;

	private JPanel titlePanel;
	private JButton addButton;

	private JButton btnAktualisieren;
	private JPanel searchPanel;

	private AddressBookTablePanel bookTable;
	private JTextField searchField;

	private Main dataManager;
	private JLabel lblAddressbuch;

	public AddressBookView(Main dataManager) {
		super();
		setResizable(false);
		this.dataManager = dataManager;
		this.initComponents();
		this.display();
	}

	public void initComponents() {

		getContentPane().setLayout(new GridLayout(0, 1));
		this.setBackground(Color.BLUE);
		this.titlePanel = new JPanel();
		titlePanel.setBorder(new LineBorder(new Color(255, 51, 0)));
		titlePanel.setBackground(new Color(51, 204, 255));
		titlePanel.setLayout(null);
		this.addButton = new JButton("");
		this.addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new AddressBookEditDialog(dataManager);
			}
		});
		addButton.setBounds(312, 10, 51, 43);
		this.addButton.setToolTipText("Neue Addresse erstellen");
		addButton.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/add_address_entry.png")));
		titlePanel.add(addButton);

		searchPanel = new JPanel();
		searchPanel.setLayout(null);

		this.bookTable = new AddressBookTablePanel(this.dataManager);

		// getContentPane().add(this.titlePanel);

		btnAktualisieren = new JButton("");
		btnAktualisieren.setBounds(373, 10, 51, 43);
		this.btnAktualisieren.setToolTipText("Aktualisieren");
		btnAktualisieren.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/refresh_addressbook.png")));
		this.btnAktualisieren.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				searchField.setText("");
				bookTable.getModel().transferFromDatabase();
			}
		});
		
		titlePanel.add(btnAktualisieren);

		lblAddressbuch = new JLabel("Addressbuch");
		lblAddressbuch.setFont(new Font("Verdana", Font.BOLD, 15));
		lblAddressbuch.setBounds(10, 10, 168, 43);
		lblAddressbuch.setIcon(new javax.swing.ImageIcon(getClass()
				.getResource("/address_book.png")));

		titlePanel.add(lblAddressbuch);
		getContentPane().add(searchPanel);

		JLabel lblSearchlabel = new JLabel("Suche:");
		lblSearchlabel.setFont(new Font("Verdana", Font.BOLD, 12));
		// lblSearchlabel.setBounds(10, 11, 65, 14);
		lblSearchlabel.setBounds(20, 70, 65, 14);
		searchPanel.add(lblSearchlabel);

		searchField = new JTextField();
		searchField.setFont(new Font("Verdana", Font.PLAIN, 11));
		searchField.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent evt) {
				autoSearch(evt);
			}

			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
		// searchField.setBounds(10, 28, 414, 20);
		searchField.setBounds(20, 87, 620, 20);
		searchPanel.add(searchField);
		// searchField.setColumns(10);

		titlePanel.setBounds(5, 5, 650, 60);
		searchPanel.add(this.titlePanel);

		// this.bookTable.setBounds(10, 55, 414, 500);
		this.bookTable.setBounds(20, 130, 620, 550);
		searchPanel.add(this.bookTable);

	}

	protected void autoSearch(KeyEvent event) {

		JTextField testField = (JTextField) event.getSource();

		int position = testField.getCaretPosition();
		if (position >= 1) {
			this.bookTable.getModel().retrieveAddressEntry(testField.getText());
		}

		if (this.searchField.getText().length() == 0) {
			this.bookTable.getModel().clearModel();
			this.bookTable.getModel().transferFromDatabase();
		}
	}

	public void display() {

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

		pack();
		setSize(700, 750);
		setLocation((int) (dim.getWidth() - this.getWidth()) / 2,
				(int) (dim.getHeight() - this.getHeight()) / 2);
		ImageIcon img = new ImageIcon(getClass()
				.getResource("/address_book.png"));
		this.setIconImage(img.getImage());
		
		setVisible(true);
	}
}
